<?php
require_once("../includes/config.php");
include DIR_INCLUDES_FT.'bootstrap.php';
header('Content-type: text/html; charset=utf-8');

Doctrine_Manager::getInstance()->setCurrentConnection('logs_connection');

$query = new Doctrine_Query();

$query->from("Transaccoes t")->innerJoin("t.Operacoes o")
		->innerJoin("o.Tabelas tab")->innerJoin("o.Accoes a")
		->innerJoin("o.CamposAlterados c");

if($_POST['tabela'] != ''){
	$query->addWhere("o.tabela_id = ?",array($_POST['tabela']));
}

if($_POST['accao'] != ''){
	$query->addWhere("a.id = ?",array($_POST['accao']));
}

if($_POST['codigo'] != ''){
	$query->addWhere("o.id_interno = ?",array($_POST['codigo']));
}

if($_POST['data_inicio'] != ''){
	$query->addWhere("DATEDIFF(DATE(t.data_inicio),?) > -1",array(FuncoesGerais::date_to_data_base($_POST['data_inicio'])));
}

if($_POST['data_fim'] != ''){
	$query->addWhere("DATEDIFF(?,DATE(t.data_fim)) > -1",array(FuncoesGerais::date_to_data_base($_POST['data_fim'])));
}

$transacoes = $query->orderBy("t.data_inicio ASC")->fetchArray();
if (sizeof($transacoes) > 0){?>
	<table class="transaccoes" cellpadding="0" cellspacing="0" align="center">
	<?foreach ($transacoes as $transacao){?>
		<tr class="table_data_title">
			<td colspan="2"><h4 style="cursor: pointer;">Transacção nº <?=$transacao['id']?></h4></td>
		</tr>
		<tr class="table_data_title">
			<td style="width: 350px;"><?=UTILIZADOR?></td>
			<td style="width: 200px;"><?=DATA?></td>
		</tr>
		<tr class="transaccoes" onclick="$(this).next('tr').toggle();">
			<td>Utilizador indefinido</td>
			<td><?=$transacao['data_inicio']?></td>
		</tr>
		<tr class="hidden">
			<td colspan="2" style="border:none;">
				<table class="operacoes" cellpadding="0" cellspacing="0" align="center">
					<?foreach ($transacao['Operacoes'] as $operacao){?>
						<thead>
							<tr class="table_data_title">
								<td><span><?=TABELA?></span></td>
								<td><span><?=ID?></span></td>
								<td><span><?=ACCAO?></span></td>
							</tr>
						</thead>
						<tr class="operacoes" onclick="$(this).next('tr').toggle();">
							<td><span><?=$operacao['Tabelas']['nome']?></span></td>
							<td><span><?=$operacao['id_interno']?></span></td>
							<td><span><?=$operacao['Accoes']['alias']?></span></td>
						</tr>
						<tr class="hidden">
							<td colspan="3" style="border:none;">
								<table class="campos_alterados" cellpadding="0" cellspacing="0" align="center">
									<tr class="table_data_title">
										<td><span><?=CAMPO?></span></td>
										 <?switch ($operacao['Accoes']['id']){
											case '1':?>
												<td><span><?=VALOR_NOVO?></span></td>
                                                    <?break;
											case '2':?>
												<td><span><?=VALOR_ANTIGO?></span></td>
												<td><span><?=VALOR_NOVO?></span></td>
											<?break;
											case '3':?>
												<td><span><?=VALOR_ANTIGO?></span></td>
											<?break;
										}?>
									</tr>
									<?foreach($operacao['CamposAlterados'] as $campo_alterado){
										switch ($operacao['Accoes']['id']){
											case '1':?>
												<tr class="campos_alterados">
													<td><span><?=$campo_alterado['campo']?></span></td>
													<td><textarea readonly="readonly" class="campos_alterados"><?=$campo_alterado['new_value']?></textarea></td>
												</tr>
											<?break;
											case '2':?>
												<tr class="campos_alterados">
													<td><span><?=$campo_alterado['campo']?></span></td>
													<td><textarea readonly="readonly" class="campos_alterados"><?=$campo_alterado['old_value']?></textarea></td>
													<td><textarea readonly="readonly" class="campos_alterados"><?=$campo_alterado['new_value']?></textarea></td>
												</tr>
											<?break;
											case '3':?>
												<tr class="campos_alterados">
													<td><span><?=$campo_alterado['campo']?></span></td>
													<td><textarea readonly="readonly" class="campos_alterados"><?=$campo_alterado['old_value']?></textarea></td>
												</tr>
											<?break;
										}?>
									<?}?>
								</table>
							</td>
						</tr>
					<?}?>
				</table>
			</td>
		</tr>
	<?}?>
	</table>
	<?
} else {?>
	<div style="height: 100px;margin-top: 40px;">Não existem resultados para mostrar</div>
<?}

Doctrine_Manager::getInstance()->setCurrentConnection('base_connection');
?>